SlideShare a Scribd company logo
1 of 54
Class No.39  Data Structures http://ecomputernotes.com
Divide and Conquer What if we split the list into two parts? 10 12 8 4 2 11 7 5 10 12 8 4 2 11 7 5 http://ecomputernotes.com
Divide and Conquer Sort the two parts: 10 12 8 4 2 11 7 5 4 8 10 12 2 5 7 11 http://ecomputernotes.com
Divide and Conquer Then merge the two parts together: 4 8 10 12 2 5 7 11 2 4 5 7 8 10 11 12 http://ecomputernotes.com
Analysis ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],http://ecomputernotes.com
Divide and Conquer ,[object Object],[object Object],[object Object],[object Object],[object Object],http://ecomputernotes.com
Search Divide and Conquer Search Search Recall:  Binary Search http://ecomputernotes.com
Sort Divide and Conquer Sort Sort Sort Sort Sort Sort http://ecomputernotes.com
Divide and Conquer Combine Combine Combine http://ecomputernotes.com
Mergesort ,[object Object],[object Object],[object Object],[object Object],[object Object],http://ecomputernotes.com
Mergesort ,[object Object],[object Object],[object Object],[object Object],http://ecomputernotes.com
Merging: animation 4 8 10 12 2 5 7 11 2 http://ecomputernotes.com
Merging: animation 4 8 10 12 2 5 7 11 2 4 http://ecomputernotes.com
Merging: animation 4 8 10 12 2 5 7 11 2 4 5 http://ecomputernotes.com
Merging 4 8 10 12 2 5 7 11 2 4 5 7 http://ecomputernotes.com
Mergesort 8 12 11 2 7 5 4 10 Split the list in half. 8 12 4 10 Mergesort the left half. Split the list in half. Mergesort the left half. 4 10 Split the list in half. Mergesort the left half. 10 Mergesort the right. 4 http://ecomputernotes.com
Mergesort 8 12 11 2 7 5 4 10 8 12 4 10 4 10 Mergesort the right half. Merge the two halves. 10 4 8 12 12 8 Merge the two halves . 8 8 12 http://ecomputernotes.com
Mergesort 8 12 11 2 7 5 4 10 8 12 4 10 Merge the two halves. 4 10 Mergesort the right half. Merge the two halves. 10 4 8 12 10 12 8 4 10 4 8 12 http://ecomputernotes.com
Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 11 2 11 2 http://ecomputernotes.com
Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 11 2 2 11 2 11 http://ecomputernotes.com
Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 2 11 7 5 7 5 http://ecomputernotes.com
Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 5 7 2 11 http://ecomputernotes.com
Mergesort 10 12 2 5 7 11 8 4 Mergesort the right half. http://ecomputernotes.com
Mergesort 5 7 8 10 11 12 4 2 Merge the two halves. http://ecomputernotes.com
void mergeSort(float array[], int size) { int* tmpArrayPtr = new int[size];  if (tmpArrayPtr != NULL)  mergeSortRec(array, size, tmpArrayPtr); else { cout << “Not enough memory to sort list.”); return; } delete [] tmpArrayPtr; } Mergesort http://ecomputernotes.com
void mergeSortRec(int array[],int size,int tmp[]) { int i; int mid = size/2; if (size > 1){ mergeSortRec(array, mid, tmp); mergeSortRec(array+mid, size-mid, tmp); mergeArrays(array, mid, array+mid, size-mid, tmp);  for (i = 0; i < size; i++)  array[i] = tmp[i]; } } Mergesort http://ecomputernotes.com
3 5 15 28 30 6 10 14 22 43 50 a: b: aSize: 5 bSize: 6 mergeArrays tmp: http://ecomputernotes.com
mergeArrays 5 15 28 30 10 14 22 43 50 a: b: tmp: i=0 k=0 j=0 3 6 http://ecomputernotes.com
mergeArrays 5 15 28 30 10 14 22 43 50 a: b: tmp: i=0 k=0 3 j=0 3 6 http://ecomputernotes.com
mergeArrays 3 15 28 30 10 14 22 43 50 a: b: tmp: i=1 j=0 k=1 3 5 5 6 http://ecomputernotes.com
mergeArrays 3 5 28 30 10 14 22 43 50 a: b: 3 5 tmp: i=2 j=0 k=2 6 15 6 http://ecomputernotes.com
mergeArrays 3 5 28 30 6 14 22 43 50 a: b: 3 5 6 tmp: i=2 j=1 k=3 15 10 10 http://ecomputernotes.com
mergeArrays 10 3 5 28 30 6 22 43 50 a: b: 3 5 6 tmp: i=2 j=2 k=4 15 10 14 14 http://ecomputernotes.com
mergeArrays 14 10 3 5 28 30 6 14 43 50 a: b: 3 5 6 tmp: i=2 j=3 k=5 15 10 22 15 http://ecomputernotes.com
mergeArrays 14 10 3 5 30 6 14 43 50 a: b: 3 5 6 tmp: i=3 j=3 k=6 15 10 22 22 15 28 http://ecomputernotes.com
mergeArrays 14 10 3 5 30 6 14 50 a: b: 3 5 6 tmp: i=3 j=4 k=7 15 10 22 28 15 28 43 22 http://ecomputernotes.com
mergeArrays 14 10 3 5 6 14 50 a: b: 3 5 6 tmp: i=4 j=4 k=8 15 10 22 30 15 28 43 22 30 28 http://ecomputernotes.com
mergeArrays 14 10 3 5 6 14 50 a: b: 3 5 6 30 tmp: i=5 j=4 k=9 15 10 22 15 28 43 22 30 28 43 50 Done. http://ecomputernotes.com
Merge Sort and Linked Lists Sort Sort Merge http://ecomputernotes.com
Mergesort Analysis Merging the two lists of size  n /2: O( n ) Merging the four lists of size n/4: O( n ) . . . Merging the n lists of size 1: O( n ) O (lg  n ) times ,[object Object],[object Object],[object Object],[object Object]
Mergesort Analysis ,[object Object],[object Object],[object Object],[object Object],http://ecomputernotes.com
Quicksort ,[object Object],[object Object],http://ecomputernotes.com
Quicksort First the list is partitioned around a pivot value. Pivot can be chosen from the beginning, end or  middle  of list): 8 3 2 11 7 5 4 10 12 4 5 5 pivot value http://ecomputernotes.com
Quicksort The pivot is swapped to the last position and the  remaining elements are compared starting at the ends. 8 3 2 11 7 5 4 10 12 4 5 low high 5 pivot value http://ecomputernotes.com
Quicksort Then the low index moves right until it is at an element  that is larger than the pivot value (i.e., it is on the wrong side) 8 6 2 11 7 5 10 12 4 6 low high 5 pivot value 3 12 http://ecomputernotes.com
Quicksort Then the high index moves left until it is at an  element that is smaller than the pivot value (i.e., it  is on the wrong side) 8 6 2 11 7 5 4 10 12 4 6 low high 5 pivot value 3 2 http://ecomputernotes.com
Quicksort Then the two values are swapped and the index values are updated: 8 6 2 11 7 5 4 10 12 4 6 low high 5 pivot value 3 2 12 http://ecomputernotes.com
Quicksort This continues until the two index values pass  each other: 8 6 12 11 7 5 4 2 4 6 low high 5 pivot value 3 10 3 10 http://ecomputernotes.com
Quicksort This continues until the two index values pass  each other: 8 6 12 11 7 5 4 2 4 6 low high 5 pivot value 10 3 http://ecomputernotes.com
Quicksort Then the pivot value is swapped into position: 8 6 12 11 7 5 4 2 4 6 low high 10 3 8 5 http://ecomputernotes.com
Quicksort Recursively quicksort the two parts: 5 6 12 11 7 8 4 2 4 6 10 3 Quicksort the left part Quicksort the right part 5 http://ecomputernotes.com
Quicksort void quickSort(int array[], int size) { int index; if (size > 1) { index = partition(array, size); quickSort(array, index); quickSort(array+index+1, size - index-1); } } http://ecomputernotes.com
int partition(int array[], int size) { int k; int mid = size/2; int index = 0; swap(array, array+mid);  for (k = 1; k < size; k++){ if (array[k] < array[0]){ index++; swap(array+k, array+index); } } swap(array, array+index); return index; } Quicksort
Data Structures-Course Recap ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],http://ecomputernotes.com

More Related Content

Similar to computer notes - Data Structures - 39

Computer notes data structures - 9
Computer notes   data structures - 9Computer notes   data structures - 9
Computer notes data structures - 9ecomputernotes
 
computer notes - Data Structures - 38
computer notes - Data Structures - 38computer notes - Data Structures - 38
computer notes - Data Structures - 38ecomputernotes
 
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)Jinho Choi
 
Computer notes - Maze Generator
Computer notes - Maze GeneratorComputer notes - Maze Generator
Computer notes - Maze Generatorecomputernotes
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Flynce Miguel
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applicationsyazad dumasia
 
computer notes - Data Structures - 33
computer notes - Data Structures - 33computer notes - Data Structures - 33
computer notes - Data Structures - 33ecomputernotes
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
computer notes - Data Structures - 25
computer notes - Data Structures - 25computer notes - Data Structures - 25
computer notes - Data Structures - 25ecomputernotes
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31ecomputernotes
 
computer notes - Data Structures - 9
computer notes - Data Structures - 9computer notes - Data Structures - 9
computer notes - Data Structures - 9ecomputernotes
 
Computer notes - Build Heap
Computer notes  - Build HeapComputer notes  - Build Heap
Computer notes - Build Heapecomputernotes
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printAbdii Rashid
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).pptyasser3omr
 

Similar to computer notes - Data Structures - 39 (20)

Computer notes data structures - 9
Computer notes   data structures - 9Computer notes   data structures - 9
Computer notes data structures - 9
 
computer notes - Data Structures - 38
computer notes - Data Structures - 38computer notes - Data Structures - 38
computer notes - Data Structures - 38
 
Sorting
SortingSorting
Sorting
 
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)
 
Computer notes - Maze Generator
Computer notes - Maze GeneratorComputer notes - Maze Generator
Computer notes - Maze Generator
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
 
computer notes - Data Structures - 33
computer notes - Data Structures - 33computer notes - Data Structures - 33
computer notes - Data Structures - 33
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
computer notes - Data Structures - 25
computer notes - Data Structures - 25computer notes - Data Structures - 25
computer notes - Data Structures - 25
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31
 
16-sorting.ppt
16-sorting.ppt16-sorting.ppt
16-sorting.ppt
 
Sorting
SortingSorting
Sorting
 
Sorting
SortingSorting
Sorting
 
computer notes - Data Structures - 9
computer notes - Data Structures - 9computer notes - Data Structures - 9
computer notes - Data Structures - 9
 
Recursion.pptx
Recursion.pptxRecursion.pptx
Recursion.pptx
 
Computer notes - Build Heap
Computer notes  - Build HeapComputer notes  - Build Heap
Computer notes - Build Heap
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
 

More from ecomputernotes

computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11ecomputernotes
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20ecomputernotes
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraintsecomputernotes
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functionsecomputernotes
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueriesecomputernotes
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objectsecomputernotes
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28ecomputernotes
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19ecomputernotes
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4ecomputernotes
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13ecomputernotes
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueriesecomputernotes
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functionsecomputernotes
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16ecomputernotes
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22ecomputernotes
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35ecomputernotes
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36ecomputernotes
 
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY ClauseComputer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY Clauseecomputernotes
 
Computer notes - Manipulating Data
Computer notes - Manipulating DataComputer notes - Manipulating Data
Computer notes - Manipulating Dataecomputernotes
 
Computer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT StatementsComputer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT Statementsecomputernotes
 
computer notes - Data Structures - 14
computer notes - Data Structures - 14computer notes - Data Structures - 14
computer notes - Data Structures - 14ecomputernotes
 

More from ecomputernotes (20)

computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraints
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functions
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueries
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objects
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueries
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functions
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36
 
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY ClauseComputer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY Clause
 
Computer notes - Manipulating Data
Computer notes - Manipulating DataComputer notes - Manipulating Data
Computer notes - Manipulating Data
 
Computer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT StatementsComputer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT Statements
 
computer notes - Data Structures - 14
computer notes - Data Structures - 14computer notes - Data Structures - 14
computer notes - Data Structures - 14
 

Recently uploaded

State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!Memoori
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform EngineeringMarcus Vechiato
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxFIDO Alliance
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingScyllaDB
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfalexjohnson7307
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxjbellis
 
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdfMuhammad Subhan
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewDianaGray10
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfSrushith Repakula
 
Microsoft BitLocker Bypass Attack Method.pdf
Microsoft BitLocker Bypass Attack Method.pdfMicrosoft BitLocker Bypass Attack Method.pdf
Microsoft BitLocker Bypass Attack Method.pdfOverkill Security
 
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?Paolo Missier
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxMarkSteadman7
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)Samir Dash
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireExakis Nelite
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsLeah Henrickson
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightSafe Software
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024Lorenzo Miniero
 

Recently uploaded (20)

State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptx
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdf
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptx
 
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overview
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
Microsoft BitLocker Bypass Attack Method.pdf
Microsoft BitLocker Bypass Attack Method.pdfMicrosoft BitLocker Bypass Attack Method.pdf
Microsoft BitLocker Bypass Attack Method.pdf
 
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - Questionnaire
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 

computer notes - Data Structures - 39

  • 1. Class No.39 Data Structures http://ecomputernotes.com
  • 2. Divide and Conquer What if we split the list into two parts? 10 12 8 4 2 11 7 5 10 12 8 4 2 11 7 5 http://ecomputernotes.com
  • 3. Divide and Conquer Sort the two parts: 10 12 8 4 2 11 7 5 4 8 10 12 2 5 7 11 http://ecomputernotes.com
  • 4. Divide and Conquer Then merge the two parts together: 4 8 10 12 2 5 7 11 2 4 5 7 8 10 11 12 http://ecomputernotes.com
  • 5.
  • 6.
  • 7. Search Divide and Conquer Search Search Recall: Binary Search http://ecomputernotes.com
  • 8. Sort Divide and Conquer Sort Sort Sort Sort Sort Sort http://ecomputernotes.com
  • 9. Divide and Conquer Combine Combine Combine http://ecomputernotes.com
  • 10.
  • 11.
  • 12. Merging: animation 4 8 10 12 2 5 7 11 2 http://ecomputernotes.com
  • 13. Merging: animation 4 8 10 12 2 5 7 11 2 4 http://ecomputernotes.com
  • 14. Merging: animation 4 8 10 12 2 5 7 11 2 4 5 http://ecomputernotes.com
  • 15. Merging 4 8 10 12 2 5 7 11 2 4 5 7 http://ecomputernotes.com
  • 16. Mergesort 8 12 11 2 7 5 4 10 Split the list in half. 8 12 4 10 Mergesort the left half. Split the list in half. Mergesort the left half. 4 10 Split the list in half. Mergesort the left half. 10 Mergesort the right. 4 http://ecomputernotes.com
  • 17. Mergesort 8 12 11 2 7 5 4 10 8 12 4 10 4 10 Mergesort the right half. Merge the two halves. 10 4 8 12 12 8 Merge the two halves . 8 8 12 http://ecomputernotes.com
  • 18. Mergesort 8 12 11 2 7 5 4 10 8 12 4 10 Merge the two halves. 4 10 Mergesort the right half. Merge the two halves. 10 4 8 12 10 12 8 4 10 4 8 12 http://ecomputernotes.com
  • 19. Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 11 2 11 2 http://ecomputernotes.com
  • 20. Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 11 2 2 11 2 11 http://ecomputernotes.com
  • 21. Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 2 11 7 5 7 5 http://ecomputernotes.com
  • 22. Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 5 7 2 11 http://ecomputernotes.com
  • 23. Mergesort 10 12 2 5 7 11 8 4 Mergesort the right half. http://ecomputernotes.com
  • 24. Mergesort 5 7 8 10 11 12 4 2 Merge the two halves. http://ecomputernotes.com
  • 25. void mergeSort(float array[], int size) { int* tmpArrayPtr = new int[size]; if (tmpArrayPtr != NULL) mergeSortRec(array, size, tmpArrayPtr); else { cout << “Not enough memory to sort list.”); return; } delete [] tmpArrayPtr; } Mergesort http://ecomputernotes.com
  • 26. void mergeSortRec(int array[],int size,int tmp[]) { int i; int mid = size/2; if (size > 1){ mergeSortRec(array, mid, tmp); mergeSortRec(array+mid, size-mid, tmp); mergeArrays(array, mid, array+mid, size-mid, tmp); for (i = 0; i < size; i++) array[i] = tmp[i]; } } Mergesort http://ecomputernotes.com
  • 27. 3 5 15 28 30 6 10 14 22 43 50 a: b: aSize: 5 bSize: 6 mergeArrays tmp: http://ecomputernotes.com
  • 28. mergeArrays 5 15 28 30 10 14 22 43 50 a: b: tmp: i=0 k=0 j=0 3 6 http://ecomputernotes.com
  • 29. mergeArrays 5 15 28 30 10 14 22 43 50 a: b: tmp: i=0 k=0 3 j=0 3 6 http://ecomputernotes.com
  • 30. mergeArrays 3 15 28 30 10 14 22 43 50 a: b: tmp: i=1 j=0 k=1 3 5 5 6 http://ecomputernotes.com
  • 31. mergeArrays 3 5 28 30 10 14 22 43 50 a: b: 3 5 tmp: i=2 j=0 k=2 6 15 6 http://ecomputernotes.com
  • 32. mergeArrays 3 5 28 30 6 14 22 43 50 a: b: 3 5 6 tmp: i=2 j=1 k=3 15 10 10 http://ecomputernotes.com
  • 33. mergeArrays 10 3 5 28 30 6 22 43 50 a: b: 3 5 6 tmp: i=2 j=2 k=4 15 10 14 14 http://ecomputernotes.com
  • 34. mergeArrays 14 10 3 5 28 30 6 14 43 50 a: b: 3 5 6 tmp: i=2 j=3 k=5 15 10 22 15 http://ecomputernotes.com
  • 35. mergeArrays 14 10 3 5 30 6 14 43 50 a: b: 3 5 6 tmp: i=3 j=3 k=6 15 10 22 22 15 28 http://ecomputernotes.com
  • 36. mergeArrays 14 10 3 5 30 6 14 50 a: b: 3 5 6 tmp: i=3 j=4 k=7 15 10 22 28 15 28 43 22 http://ecomputernotes.com
  • 37. mergeArrays 14 10 3 5 6 14 50 a: b: 3 5 6 tmp: i=4 j=4 k=8 15 10 22 30 15 28 43 22 30 28 http://ecomputernotes.com
  • 38. mergeArrays 14 10 3 5 6 14 50 a: b: 3 5 6 30 tmp: i=5 j=4 k=9 15 10 22 15 28 43 22 30 28 43 50 Done. http://ecomputernotes.com
  • 39. Merge Sort and Linked Lists Sort Sort Merge http://ecomputernotes.com
  • 40.
  • 41.
  • 42.
  • 43. Quicksort First the list is partitioned around a pivot value. Pivot can be chosen from the beginning, end or middle of list): 8 3 2 11 7 5 4 10 12 4 5 5 pivot value http://ecomputernotes.com
  • 44. Quicksort The pivot is swapped to the last position and the remaining elements are compared starting at the ends. 8 3 2 11 7 5 4 10 12 4 5 low high 5 pivot value http://ecomputernotes.com
  • 45. Quicksort Then the low index moves right until it is at an element that is larger than the pivot value (i.e., it is on the wrong side) 8 6 2 11 7 5 10 12 4 6 low high 5 pivot value 3 12 http://ecomputernotes.com
  • 46. Quicksort Then the high index moves left until it is at an element that is smaller than the pivot value (i.e., it is on the wrong side) 8 6 2 11 7 5 4 10 12 4 6 low high 5 pivot value 3 2 http://ecomputernotes.com
  • 47. Quicksort Then the two values are swapped and the index values are updated: 8 6 2 11 7 5 4 10 12 4 6 low high 5 pivot value 3 2 12 http://ecomputernotes.com
  • 48. Quicksort This continues until the two index values pass each other: 8 6 12 11 7 5 4 2 4 6 low high 5 pivot value 3 10 3 10 http://ecomputernotes.com
  • 49. Quicksort This continues until the two index values pass each other: 8 6 12 11 7 5 4 2 4 6 low high 5 pivot value 10 3 http://ecomputernotes.com
  • 50. Quicksort Then the pivot value is swapped into position: 8 6 12 11 7 5 4 2 4 6 low high 10 3 8 5 http://ecomputernotes.com
  • 51. Quicksort Recursively quicksort the two parts: 5 6 12 11 7 8 4 2 4 6 10 3 Quicksort the left part Quicksort the right part 5 http://ecomputernotes.com
  • 52. Quicksort void quickSort(int array[], int size) { int index; if (size > 1) { index = partition(array, size); quickSort(array, index); quickSort(array+index+1, size - index-1); } } http://ecomputernotes.com
  • 53. int partition(int array[], int size) { int k; int mid = size/2; int index = 0; swap(array, array+mid); for (k = 1; k < size; k++){ if (array[k] < array[0]){ index++; swap(array+k, array+index); } } swap(array, array+index); return index; } Quicksort
  • 54.

Editor's Notes

  1. End of lecture 44.
  2. Start of lecture 45
  3. End of lecture 45. At last!